/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.mailfolder;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.mailfolder.Mail;
import com.unis.service.attach.AttachmentService;
import com.unis.service.mailfolder.MailService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-02-21
 */
@Controller
@RequestMapping("/mail")
public class MailController {

    private MailService mailService;
    private AttachmentService attachmentService;

    /****************************************
     * Insert a instace of Mail
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertMail(@RequestBody Mail mail) throws Exception {
        mail.setStatmentId("Mail.insert");
        String status = "failure";
        mailService.insert(mail);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Mail
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Mail mail) throws Exception {
        mail.setStatmentId("Mail.update");
        String status = "failure";
        mailService.update(mail);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Mail
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Mail mail) throws Exception {
        mail.setStatmentId("Mail.delete");
        String status = "failure";
        mailService.batchDelete(mail);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Mail
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Mail mail = new Mail();
        mail.setStatmentId("Mail.queryAsList");
        BeanUtils.populate(mail, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(mail, pageMap);
        List<BaseModel> mailList = mailService.queryAsList(mail);
        Map<String, Object> renderMap = GridUIMapper.render(mail, mailList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Mail
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Mail mail) throws Exception {
        mail.setStatmentId("Mail.queryAsObject");
        return mailService.queryAsObject(mail);
    }

    @RequestMapping("/queryMailAttach")
    @ResponseBody
    public Object queryMailAttach(Mail mail) throws Exception {
        AttachLink attachlink = new AttachLink();
        attachlink.setEntityid(mail.getMailid());
        return attachmentService.querybyEntityid(attachlink);
    }

    @RequestMapping("/page")
    public String forword() {
        return "mailfolder/mailList";
    }

    @RequestMapping("/newmail")
    public String newmail() {
        return "mailfolder/newMailForm";
    }

    @Resource
    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

    @Resource
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
}
